// JavaScript Document

dojo.require("com.otupia.JSConstants");
dojo.require("com.otupia.ModuleUtils");
dojo.provide("com.otupia.ModuleLoader");

com.otupia.ModuleLoader = function(onLoadCallBack)
{
	var defualtCallBack = function(module)
	{
		console.log("module : " + module+" loaded!!");
		console.log("detail : " + dojo.toJson(module));
	};
	this.onLoadCallBack = onLoadCallBack || defualtCallBack;
};
com.otupia.ModuleLoader.prototype.load = function(context,moduleName)
{
	var reqUrl = com.otupia.ModuleUtils.getModuleUrl(moduleName);
	
	var getCallBack = function(self)
	{
		return function callBack_temp(data)
		{
			var tempObj = {};
			eval("tempObj = " + data);
			tempObj["fatherNode"] = context;
			
			if(tempObj["onLoad"] instanceof Function)
			{
				tempObj.onLoad();
			}
			self.onLoadCallBack(tempObj);
		};
	}(this);
	
	dojo.xhrGet(
		{
			url: reqUrl,
			load : getCallBack,
			error : function(response){alert(response);}
		}
	);
};
/*
com.otupia.ModuleListLoader.prototype.checkState = function()
{
	var checked = true;
	if(this.moduleList instanceof Array)
	{
		for(var ind=0;ind < this.moduleList.length;ind++)
		{
			if(this.moduleList[ind])
			{
				checked = false;
				break;
			}
		}
	}
	else if(typeof this.moduleList == "object")
	{
		checked = false;
	}
	
	if(checked)
	{
		dojo.disconnect(this.connecter);
		this.moduleList = null;
		this.state = "loaded";
	}
};

com.otupia.ModuleListLoader.prototype.checkCallBack = function(module)
{
	console.log("com.otupia.ModuleListLoader.prototype.checkCallBack and module is : " + module.name);
	
	if(this.moduleList instanceof Array)
	{
		for(var ind=0;ind < this.moduleList.length;ind++)
		{
			console.log("this.moduleList[ind].name " + this.moduleList[ind].name);
			if(this.moduleList[ind] && this.moduleList[ind].name == module.name)
			{
				var meth = this.moduleList[ind].callBackMethod;
				
				if(meth)
				{
					module[meth](this.moduleList[ind]);
				}
				else
				{
					if(module[com.otupia.JSConstants._defualtExecurateMethod])
					{
						module.init(this.moduleList[ind]);
					}
						
				}
				break;
			}
		}
	}
	else if(typeof this.moduleList == "object")
	{
		if(this.moduleList && this.moduleList.name == module.name)
		{
			var meth = this.moduleList.callBackMethod || com.otupia.JSConstants._defualtExecurateMethod;
			module[meth](this.moduleList);
		}
	}
	//
	
	if(this.registerCallBack instanceof Function)
	{
		this.registerCallBack(module);
	}
	
	this.checkState();
};
*/
